﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Autorent.Mobile.Views;
using System.ComponentModel;
using Autorent.Mobile.Model;

namespace Autorent.Mobile.ViewModels
{
    public class OrderInfoViewModel : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;

        public delegate void SuccessUpdateHandler();

        public event SuccessUpdateHandler SuccessUpdate;


        public OrderInfoViewModel(string vehicleId)
        {
            this._repository = new OrderInfoRepository();
            this._vehicleId = vehicleId;
            this._repository.GetCarCompleted += new OrderInfoRepository.GetCarNameCompletedHandler(_repository_GetCarCompleted);
        }



        public string CarModel
        {
            get
            {
                if (_carName == null)
                {
                    _repository.GetAutoNameById(this._vehicleId);
                    return "processing...";
                }
                return _carName;
            }
            set
            {
                this._carName = value;
                OnPropertyChanged("CarModel");
            }
        }



        public DateTime DateReturn
        {
            get
            {
                if (_dateReturn == DateTime.MinValue)
                    _dateReturn = DateTime.Now;
                return _dateReturn;
            }
            set
            {
                this._dateReturn = value;
                OnPropertyChanged("DataReturn");
            }
        }



        public int GasQuantity
        {
            get
            {
                return this._gasQuantity;
            }
            set
            {
                this._gasQuantity = value;
                OnPropertyChanged("GasQuantity");
            }
        }



        public string DamageDescription
        {
            get
            {
                return this._damageDescription;
            }
            set
            {
                this._damageDescription = value;
                OnPropertyChanged("DamageDescription");
            }
        }



        public int DamageCost
        {
            get
            {
                return this._damageCost;
            }
            set
            {
                this._damageCost = value;
                OnPropertyChanged("DamageCost");
            }
        }



        public OrderInfoSaveCommand SaveCommand
        {
            get
            {
                _saveCommand = new OrderInfoSaveCommand(param => SaveOrder());
                return _saveCommand;
            }
        }





        #region Private Members

        private OrderInfoRepository _repository;
        private string _vehicleId;
        private string _carName;
        private DateTime _dateReturn;
        private int _gasQuantity;
        private OrderInfoSaveCommand _saveCommand;
        private string _damageDescription;
        private int _damageCost;


        private void OnPropertyChanged(string propertyName)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            PropertyChangedEventHandler pceHandler = PropertyChanged;
            if (pceHandler != null) pceHandler(this, e);
        }



        private void _repository_GetCarCompleted(string carName)
        {
            this.CarModel = carName;
        }



        private void SaveOrder()
        {
            if (!string.IsNullOrEmpty(_damageDescription)
                && !Int32.Equals(_damageCost, 0)
                && !Int32.Equals(_gasQuantity, 0))
            {
                try
                {
                    //_repository.SetDamage(_damageDescription, _damageCost, _vehicleId);
                    //_repository.SetGasQuantity(_gasQuantity, _vehicleId);
                     _repository.SetReturnDate(_dateReturn, _vehicleId);
                    if (SuccessUpdate != null)
                        SuccessUpdate();
                }
                catch (Exception)
                {
                    MessageBox.Show("Sorry, try to connect in a few minutes or contact the technical support");
                }
            }
        }


        #endregion


    }
}
